#ifndef SERIALWORKER_H
#define SERIALWORKER_H

#include <QObject>
#include <QSerialPort>
#include <QThread>
#include <QDebug>
#include "QTimer"
#include "QMutex"
#include <QMessageBox>
#include <QQueue>
class SerialWorker : public QObject
{
    Q_OBJECT
public:
    explicit SerialWorker(int serial_port,const QString &portName, QObject *parent = nullptr);

    void serial_init();

    ~SerialWorker();

    // 获取单例实例（返回指针类型）
    static SerialWorker* instance(int serial_port, const QString &portName);
    // 确保单例模式下的实例是线程安全
    static void releaseInstance(); // 释放单例实例

    void SerialSet(int serial_port,const QString &portName);

    bool stop_static = false;

signals:

    void dataReceived(QByteArray data);
    void finished(); // 新增

    //与主线程交互信号(弹窗)
signals:

    void showMessage(const QString &message);
    void display_data(const QByteArray rece);




public slots:
    void doDataSendWork(const QByteArray data);
    void doDataReciveWork();

    void onProcessingComplete(const QByteArray resultFrame);

    void start();

    // void DataSendTestWork(); // 测试

    void stop_serial();




    //
public:
    // 类中创建串口指针
    QSerialPort *serial;

    QString portName_1;
private:
    static SerialWorker *instance_;

public:
    void handleSerialData();



private:
    QByteArray resultFrame;
    int serial_port;
    QQueue<QByteArray> send_array;


};

#endif // SERIALWORKER_H
